package com.check.operation.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.check.operation.R;
import com.check.operation.model.Gfpoints;
import com.check.operation.model.Gftype;
import com.check.operation.model.MessagRe;
import com.check.operation.model.Message;
import com.check.operation.model.RequestUse;
import com.check.operation.model.Ssroom;
import com.check.operation.model.User;
import com.check.operation.util.PlaySoundPool;
import com.szj.util.HttpUtil;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.OnHierarchyChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class HjgyAdapter extends BaseAdapter implements OnClickListener,OnItemSelectedListener,OnHierarchyChangeListener {
	private LayoutInflater layoutInflater;
	private User user;
	private List<Ssroom> ssrooms;
	private List<Gftype> gftypes;
	PlaySoundPool playSoundPool;

	public HjgyAdapter(LayoutInflater layoutInflater, User user,List<Ssroom> ssrooms, List<Gftype> gftypes) {
		super();
		this.layoutInflater = layoutInflater;
		this.user = user;
		this.ssrooms = ssrooms;
		this.gftypes = gftypes;
		playSoundPool=new PlaySoundPool(layoutInflater.getContext());
		playSoundPool.loadSfx(R.raw.ok, 1);
		playSoundPool.loadSfx(R.raw.error, 2);
	}

	@Override
	public int getCount() {
		return ssrooms.size();
	}

	@Override
	public Ssroom getItem(int position) {
		return ssrooms.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		Ssroom ssroom = getItem(position);
		ViewHolder holder = null;
		view = layoutInflater.inflate(R.layout.gflist, null);
		holder = new ViewHolder();
	
		holder.ssfj = (TextView) view.findViewById(R.id.ssfja);
		holder.les = (TableLayout) view.findViewById(R.id.les);
		
		for(Gftype gftype:gftypes){
			TableRow tr=new TableRow(layoutInflater.getContext());
			TextView t1=new TextView(layoutInflater.getContext());
			t1.setText(gftype.TypeName);
			Spinner spinner=new Spinner(layoutInflater.getContext(),Spinner.MODE_DROPDOWN);
			ArrayAdapter<Gfpoints> adapter=new ArrayAdapter<Gfpoints>(layoutInflater.getContext(),android.R.layout.simple_spinner_item, gftype.gfpoints);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(adapter);
			DateHolder dateHolder=new DateHolder();
			dateHolder.ssroom=ssroom;
			dateHolder.gftype=gftype;
			spinner.setTag(dateHolder);
			//spinner.setOnClickListener(this);
			spinner.setOnItemSelectedListener(this);
		//	spinner.setOnHierarchyChangeListener(this);
			tr.addView(t1);
			tr.addView(spinner);
			holder.les.addView(tr);
		}
		holder.ssfj.setText(ssroom.name);
		return view;
	}

	static class ViewHolder {
		TextView ssfj;
		TableLayout les;
	}
	
	class DateHolder {
		Ssroom ssroom;//手房间
		Gftype gftype;
	}
	

	@Override
	public void onClick(View v) {
		playSoundPool.play(1, 0);
	}

	
	
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
		DateHolder dateHolder=(DateHolder) parent.getTag();
		Ssroom ssroom=dateHolder.ssroom;
		Gfpoints gfpoints=dateHolder.gftype.gfpoints.get(position);
		
		if(!"0".equals(gfpoints.Code)){
			RequestUse requestUse=new RequestUse();
			requestUse.user=this.user;
		 	Map<String,Object> params1=new HashMap<String, Object>();
		 	params1.put("PointType", gfpoints.TypeCode);
	 	    params1.put("PointID", gfpoints.Code);
	 	    params1.put("RoomCode", ssroom.code);
	 	    params1.put("RoomName",ssroom.name);
	 	    requestUse.request=params1;
			String action="workpoint/exec";
			String rs=HttpUtil.postJSON(action, JSONObject.toJSONString(requestUse));
			if(rs.indexOf("message")>0){
				MessagRe<String> messagRe=JSONObject.parseObject(rs, new TypeReference<MessagRe<String>>(){});
				if("success".equals(messagRe.message.code)){
					playSoundPool.play(1, 0);
					alert(messagRe.message.text);
				}else{
					playSoundPool.play(2, 0);
					alert(messagRe.message.text);
				}
			}else{
				Message message=JSONObject.parseObject(rs, Message.class);
				alert(message.text);
			}
			parent.setSelection(0);
		}
	}
	
	private void alert(String message){
		Toast.makeText(layoutInflater.getContext(), message, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {
	}

	@Override
	public void onChildViewAdded(View parent, View child) {
	}

	@Override
	public void onChildViewRemoved(View parent, View child) {
	}


	
}
